package com.huike.clues.service.impl;

import com.huike.clues.mapper.SysDictTypeMapper;
import com.huike.clues.service.ISysDictService;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.entity.SysDictType;
import com.huike.common.core.redis.RedisCache;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.poi.ExcelUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Service
public class SysDictServiceImpl implements ISysDictService {
    @Resource
    SysDictTypeMapper sysDictTypeMapper;
    @Resource
    RedisCache redisCache;
    @Override
    public void insertDict(SysDictType sysDictType) {
        //字段填充
        String name = SecurityUtils.getUsername();
        Date date = DateUtils.getNowDate();
        sysDictType.setCreateBy(name);
        sysDictType.setCreateTime(date);
        sysDictType.setUpdateBy(name);
        sysDictType.setUpdateTime(date);
        sysDictTypeMapper.insertDict(sysDictType);
    }

    @Override
    public void updateDict(SysDictType sysDictType) {
        //字段填充
        String name = SecurityUtils.getUsername();
        Date date = DateUtils.getNowDate();
        sysDictType.setUpdateBy(name);
        sysDictType.setUpdateTime(date);
        sysDictTypeMapper.updateDict(sysDictType);
    }

    @Override
    public void clearCache() {
        Collection<String> keys = redisCache.keys("sys_config*");
        if(keys != null && keys.size() != 0){
            redisCache.deleteObject(keys);
        }
    }

    @Override
    public AjaxResult export() {
        List<SysDictType> list = sysDictTypeMapper.findAll();
        ExcelUtil<SysDictType> excel = new ExcelUtil<>(SysDictType.class);
        return excel.exportExcel(list,"字典类型相关数据");
    }
    @Override
    public List<SysDictType> pageList(SysDictType sysDictType) {
        return sysDictTypeMapper.pageList(sysDictType);
    }

    @Override
    public void delete(List<Long> dictIds) {
        for (Long dictId : dictIds) {
            if (dictId == 0)
                sysDictTypeMapper.deleteByDictId(dictId);
        }

    }

    @Override
    public SysDictType findByDictId(Long dictId) {
        SysDictType sysDictType =  sysDictTypeMapper.findById(dictId);
        sysDictType.getDictName();
        return sysDictType;
    }
    @Override
    public SysDictType optionSelect() {
        SysDictType sysDictType = sysDictTypeMapper.optionSelect();
        SysDictType sysDictType1 = new SysDictType();
        sysDictType1.setRemark(sysDictType.getRemark());
        return sysDictType1;
    }
}
